package com.ailesson.video.service.impl;

import com.ailesson.common.except.GlobalErrorCodeConstants;
import com.ailesson.common.except.ServiceException;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import com.ailesson.video.mapper.VideoMapper;
import com.ailesson.video.entity.VideoEntity;
import com.ailesson.video.service.VideoService;
import com.ailesson.video.model.req.VideoQueryReq;
import com.ailesson.video.model.req.VideoReq;
import com.ailesson.video.model.reply.VideoReply;
import com.ailesson.common.reply.PageResult;
import cn.hutool.core.collection.CollectionUtil;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Slf4j
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper, VideoEntity> implements VideoService {

    @Value("${ali.video.subAppId:122}")
    private String subAppId;
    @Override
    public PageResult queryPage(VideoQueryReq params) {
       Page pageTmp = Page.of(params.getPageNo(),params.getPageSize());
       IPage<VideoEntity> page = this.page(
               pageTmp,
               new LambdaQueryWrapper<VideoEntity>().orderByDesc(VideoEntity::getId)
       );
       List<VideoReply> replyList = Collections.EMPTY_LIST;
       if (CollectionUtil.isNotEmpty(page.getRecords())){
           replyList = page.getRecords().stream().map(x -> {
               VideoReply reply = new VideoReply();
               BeanUtils.copyProperties(x, reply);
               return reply;
           }).collect(Collectors.toList());
       }
       return new PageResult<>(replyList,page.getTotal());
    }

    @Override
    public VideoReply info(Long id) {
        VideoEntity entity = this.getById(id);
        if (entity != null){
            VideoReply reply = new VideoReply();
            BeanUtils.copyProperties(entity, reply);
            return reply;
        }
        return null;
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void update(VideoReq video) {
        if (video.getId() == null || video.getId() <= 0){
            throw new ServiceException(GlobalErrorCodeConstants.ID_EMPTY_ERROR);
        }
        VideoEntity entity = this.getById(video.getId());
        if (entity == null){
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR);
        }
        BeanUtils.copyProperties(video, entity);
        this.updateById(entity);
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public Long add(VideoReq video) {
        video.setId(null);
        VideoEntity entity = new VideoEntity();
        BeanUtils.copyProperties(video, entity);
        this.save(entity);
        return entity.getId();
    }

    @Transactional(
            rollbackFor = {Exception.class}
    )
    @Override
    public void deleteByCourseId(Long id) {
       this.remove(new LambdaUpdateWrapper<VideoEntity>().eq(VideoEntity::getCourseId,id));
    }

    @Override
    public Map<String, Object> getPlayAuth(Long courseId, Long videoId) {
        //根据小节id获取小节对象，获取云视频id
        VideoEntity video = this.getById(videoId);
        if(video == null) {
            throw new ServiceException(GlobalErrorCodeConstants.NOT_EXISTS_ERROR.getCode(),"小节信息不存在");
        }
        Map<String, Object> map = new HashMap<>();
        map.put("videoSourceId",video.getVideoSourceId());
        map.put("appId",subAppId);
        return map;
    }
}